package com.kongfanyu.springcloud.service.impl;

import com.kongfanyu.springcloud.dao.OrderDao;
import com.kongfanyu.springcloud.domain.Order;
import com.kongfanyu.springcloud.service.AccountService;
import com.kongfanyu.springcloud.service.OrderService;
import com.kongfanyu.springcloud.service.StorageService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * Copyright (C), 2018-2020
 * FileName: OrderServiceImpl
 * Author:   kongfanyu
 * Date:     2020/9/6 12:42
 */
@Slf4j
@Service
public class OrderServiceImpl implements OrderService {

    @Resource
    private OrderDao orderDao;
    @Resource
    private StorageService storageService;
    @Resource
    private AccountService accountService;

    @Override
    @GlobalTransactional
    public void create(Order order) {
        //1.新建订单
        log.info("=====>开始新建订单");
        orderDao.create(order);

        //3.扣减账户金额
        log.info("=====>订单微服务开始调用账户微服务，做扣减开始money");
        accountService.decrease(order.getUserId(),order.getMoney());
        log.info("=====>订单微服务开始调用账户微服务，做扣减结束money");


        //2.扣减库存
        log.info("=====>订单微服务开始调用库存微服务，做扣减开始productId=" +order.getProductId()+ ",count="+order.getCount());
        storageService.decrease(order.getProductId().intValue(),order.getCount());
        log.info("=====>订单微服务开始调用库存微服务，做扣减结束count");


        //4.修改订单状态 从0到1， 1代表已经完成
        log.info("=====>修改订单状态开始");
        orderDao.update(order.getUserId(),1);
        log.info("=====>修改订单状态结束");

        log.info("=====>下订单结束，O(∩_∩)O哈哈~");
    }
}
